package com.fifth.apartment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.apartment.entity.Contract;
import com.fifth.apartment.result.Result;
import com.fifth.apartment.service.ContractService;
import com.fifth.apartment.vo.PageQueryVO;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/4
 */
@RestController
@RequestMapping("admin/contract")
@Api(tags ="合同管理接口")
@Slf4j
public class ContractController {

    @Autowired
    private ContractService service;


    @GetMapping("findPage")
    @ApiOperation("合同组分页查询")
    public Result findPage(PageQueryVO vo){

        try {
            Page<Contract> page =  service.findpage(vo);
            return  Result.success(page);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    @GetMapping("getContract")
    @ApiOperation("按照id查询合同信息")
    public Contract getContract(Integer id){
        Contract contract = service.getContract(id);
        return contract;
    }

        @PutMapping("updateContract")
    @ApiOperation("根据表单数据修改合同信息")
    public Result editContract(@RequestBody Contract contract){

        try {
            boolean   flag = service.updateById(contract);
            if (flag) {
                return new Result(true,"修改成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
       return new Result(false,"修改失败");
    }

    @DeleteMapping("deleteContract")
    @ApiOperation("根据id删除合同项")
    public Result deleteContract(Integer id){
        try {
            Boolean flag = service.deleteById(id);
            if (flag){
                return new Result(true,"删除成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new Result(false,"删除失败");
    }

    @PostMapping("addContract")
    @ApiOperation("新增合同项")
    public  Result addContract(@RequestBody Contract contract){
        try {
            Boolean flag = service.insertContract(contract);
            if (flag){
                return  new Result(true,"新增成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new Result(false,"新增失败");
    }

    @PostMapping("upload")
    @ApiOperation("合同图片上传")
    public Result upload(MultipartFile imgFile) throws IOException {
        //返回的是图片名字，拼接上七牛云的域名进行显示
        String pic = service.uploadByAliyun(imgFile);

        //返回的是外链地址，可用于直接显示
        //String pic = setmealService.uploadByAliyun(imgFile);
        if(StringUtils.hasLength(pic)){
            return new Result(true , "上传成功" , pic);
        }
        return new Result(false , "上传失败",imgFile);
    }

    @GetMapping("uploadById")
    @ApiOperation("根据id修改图片数据位置")
    public Result uploadById( String url,Integer id) throws IOException {
        Boolean flag = service.uploadById(url, id);

        //返回的是外链地址，可用于直接显示

        if(flag){
            return new Result(true , "上传成功" );
        }
        return new Result(false , "上传失败");
    }

    @GetMapping("viewById")
    @ApiOperation("根据id获取图片数据位置")
    public Result viewById( Integer id) throws IOException {
        String url = service.viewById( id);

        //返回的是外链地址，可用于直接显示

        if(StringUtils.hasLength(url)){
            return new Result(true , "获取成功",url );
        }
        return new Result(false , "获取失败");
    }
}
